﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Architecture.Presentation.Interfaces;

namespace AddIn.EA.Architecture.Presentation.Controls
{
    public partial class BEDetails : UserControl, IAddNewBusinessEntity
    {
        private BusinessEntity _entity;

        public BEDetails()
        {
            InitializeComponent();
            this.Load += new EventHandler(BEDetails_Load);
        }

        public event Action<bool> EntityValidated;
        public event Action FirstTabIndex;
        public event Action LastTabIndex;

        void BEDetails_Load(object sender, EventArgs e)
        {
            TabOrder = new Common.ControlTabOrder();
            TabOrder.OnLastTabIndex += new EventHandler<Common.TabIndexEventArgs>(TabOrder_OnLastTabIndex);
            TabOrder.OnFirstTabIndex += new EventHandler<AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs>(TabOrder_OnFirstTabIndex);
            TabOrder.Add(txtBEName);
            TabOrder.Add(txtNotes);
            TabOrder.Add(chkFilterEntity);
            TabOrder.Add(chkFilterKey);

            this.Entity.HasFilter = true;
            this.Entity.HasKeyFilter = true;
            businessEntityBindingSource.DataSource = this.Entity;
            this.txtNotes.AcceptsTab = false;
            this.txtNotes.AcceptsReturn = true;

            this.GotFocus += new EventHandler(BEDetails_GotFocus);
        }

        void BEDetails_GotFocus(object sender, EventArgs e)
        {
            TabOrder.MoveToFirst();
        }

        void TabOrder_OnFirstTabIndex(object sender, AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs e)
        {
            if (FirstTabIndex != null)
                FirstTabIndex();
            else
                TabOrder.MoveToLast();
        }

        void TabOrder_OnLastTabIndex(object sender, Common.TabIndexEventArgs e)
        {
            if (LastTabIndex != null)
                LastTabIndex();
            else
                TabOrder.MoveToFirst();
        }

        private IAddNewBusinessEntity BEWizard
        {
            get 
            {
                try
                {
                    return (IAddNewBusinessEntity)this.Parent.Parent.Parent;
                }
                catch { return null; }
            }
        }

        private Common.ControlTabOrder TabOrder { get; set; }

        #region IAddNewBusinessEntity Members

        public BusinessEntity Entity
        {
            get
            {
                if (_entity == null && BEWizard != null)
                    _entity = BEWizard.Entity;

                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        public void RefreshEntity()
        {
            if(BEWizard != null)
                BEWizard.RefreshEntity();
        }
        #endregion

        private void businessEntityBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            this.RefreshEntity();

            ValidateEntity();            
        }

        private void ValidateEntity()
        {
            if (this.EntityValidated != null)
                this.EntityValidated(txtBEName.Text.Trim().Length > 0);
        }

        
    }
}
